#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
html解析测试 - 方法选择器
"""

import re
from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>
    ddd
<a href="http://example.com/lacie" class="sister1" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister2" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
<span>中文</span>
"""



def main():
    """
    测试
    :return:
    """
    soup = BeautifulSoup(html_doc, "lxml")
    print(type(soup))

    print('*' * 40)
    # 查找所有的span标签
    print(soup.find_all('span'))

    print('*' * 40)
    # 查询所有的a标签，并且属性增加标签过滤
    print(soup.find_all('a', id='link1'))
    # 查询所有的a标签，并且增加多个属性过滤项
    print(soup.find_all('a', attrs={'class': 'sister2', 'id': 'link3'}))
    # 根据class进行过滤，此处传入的参数是**kwargs
    print(soup.find_all('a', class_='sister2'))

    print('*' * 40)
    # 通过正则表达式过滤
    print(soup.find_all(text=re.compile('Tillie')))
    # 通过limit限制输出数量
    print(soup.find_all('a', limit=2))




if __name__ == '__main__':
    main()